package com.wyw.mahjong.client.handler;

import com.wyw.mahjong.client.render.MahjongRender;
import com.wyw.mahjong.client.render.MahjongRenderManager;
import com.wyw.mahjong.common.protocol.MahjongMessage;
import com.wyw.mahjong.common.protocol.MahjongProtocol;
import com.wyw.mahjong.common.protocol.MahjongProtocolHeader;
import com.wyw.mahjong.common.util.MahjongContext;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @Title   渲染器Handler
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2024/5/6 18:06
 * Update History:
 * Author        Time            Content
 */
@Slf4j
public class MahjongClientHandler extends SimpleChannelInboundHandler<MahjongProtocol> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MahjongProtocol mahjongProtocol) throws Exception {
        // 协议头
        MahjongProtocolHeader header = mahjongProtocol.getHeader();
        // 协议体
        MahjongMessage message = (MahjongMessage) mahjongProtocol.getBody();
        // 查找渲染器
        MahjongRender mahjongRender = MahjongRenderManager.choose(message);
        if (mahjongRender != null) {
            MahjongContext.currentContext().setCurrentChannel(ctx.channel());
            mahjongRender.render(message);
        } else {
            log.error("not found render, msgType={}", message.getClass().getName());
        }
    }
}

